﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using ERP_RORZE_SYSTEM;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ERP_RORZE_DLL.BasicList
{
    public partial class frm_Add_UnitExchange : Form
    {
        private INISettings mySetting;
        private SqlConnection myConnection;
        private string error;

        private DataSet ds;
        private DataViewManager dsView;

        public frm_Add_UnitExchange()
        {
            InitializeComponent();

            this.mySetting = new INISettings();
            this.myConnection = new SqlConnection(mySetting.SQLConnectionString);
            this.error = null;
            this.ds = new DataSet("Part");
            LoadPartList();
            LoadUnitList();

            this.buyingExchange.Text = "1";
            this.usingExchange.Text = "1";
        }

        /////////////////////////
        /// Load Part List
        /////////////////////////
        private void LoadPartList()
        {
            try
            {
                myConnection.Open();
                SqlCommand cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT RTRIM(LTRIM(t1.PartNo)) AS PartNo, t1.IQID, t2.UnitID"
                    + " FROM [222_Part] AS t1 LEFT OUTER JOIN"
                    + " [222_IQ] AS t2 ON t1.IQID = t2.IQID"
                    + " ORDER BY RTRIM(LTRIM(t1.PartNo))";
                SqlDataAdapter da1 = new SqlDataAdapter();
                da1.SelectCommand = cmd_sql;
                da1.TableMappings.Add("Table", "Part");
                da1.Fill(ds);
                dsView = ds.DefaultViewManager;
                cmd_sql.Dispose();
                myConnection.Close();

                this.partNo.DataSource = dsView;
                this.partNo.DisplayMember = "Part.PartNo";
                this.partNo.ValueMember = "Part.PartNo";
                this.partNo.AutoCompleteSource = AutoCompleteSource.ListItems;

                this.iqID.DataBindings.Add("Text", dsView, "Part.IQID");
                this.iqUnit.DataBindings.Add("Text", dsView, "Part.UnitID");
            }
            catch //(Exception ex)
            {
                if (myConnection.State == ConnectionState.Open)
                {
                    myConnection.Close();
                }
                //MessageBox.Show(ex.Message);
                MessageBox.Show("Cannot get part list");
            }
        }

        /////////////////////////
        /// Load Unit List
        /////////////////////////
        private void LoadUnitList()
        {
            try
            {
                myConnection.Open();
                SqlCommand cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT UnitID FROM [222_Unit] ORDER BY UnitID";
                SqlDataReader dataReader = cmd_sql.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Value = dataReader["UnitID"].ToString();
                    item.Text = dataReader["UnitID"].ToString();
                    this.buyingUnit.Properties.Items.Add(item);
                    this.usingUnit.Properties.Items.Add(item);
                }
                dataReader.Close();
                cmd_sql.Dispose();
                myConnection.Close();
            }
            catch //(Exception ex)
            {
                if (myConnection.State == ConnectionState.Open)
                {
                    myConnection.Close();
                }
                //MessageBox.Show(ex.Message);
                MessageBox.Show("Cannot get unit list");
            }
        }
        
        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            error = null;

            string part_no = null;
            string using_unit = null;
            string buying_unit = null;
            float using_exchange = 0;
            float buying_exchange = 0;
            
            ComboboxItem item = new ComboboxItem();

            if (this.partNo.SelectedIndex < 0)
            {
                error = "Please select PartNo";
            }
            else
            {                
                part_no = this.partNo.Text.Trim();
            }

            if (string.IsNullOrEmpty(error))
            {
                if (this.buyingUnit.SelectedIndex < 0)
                {
                    error = "Please select Buying Unit";
                }
                else
                {
                    item = (ComboboxItem)this.buyingUnit.SelectedItem;
                    buying_unit = item.Value.ToString();
                }
            }

            if (string.IsNullOrEmpty(error))
            {
                if (this.usingUnit.SelectedIndex < 0)
                {
                    error = "Please select Using Unit";
                }
                else
                {
                    item = (ComboboxItem)this.usingUnit.SelectedItem;
                    using_unit = item.Value.ToString();
                }
            }

            if (string.IsNullOrEmpty(error))
            {
                if (string.IsNullOrEmpty(this.usingExchange.Text))
                {
                    error = "Please enter Using/IQEx";
                }
                else
                {
                    if (float.TryParse(this.usingExchange.Text.Trim(), out using_exchange))
                    {
                        if (using_exchange <= 0)
                        {
                            error = "Using/IQEx must be greater than 0";
                        }
                    }
                    else
                    {
                        error = "Invalid Using/IQEx";
                    }
                }
            }

            if (string.IsNullOrEmpty(error))
            {
                if (string.IsNullOrEmpty(this.buyingExchange.Text))
                {
                    error = "Please enter Buying/IQEx";
                }
                else
                {
                    if (float.TryParse(this.buyingExchange.Text.Trim(), out buying_exchange))
                    {
                        if (buying_exchange <= 0)
                        {
                            error = "Buying/IQEx must be greater than 0";
                        }
                    }
                    else
                    {
                        error = "Invalid Buying/IQEx";
                    }
                }
            }

            if (string.IsNullOrEmpty(error))
            {   
                try
                {
                    myConnection.Open();
                    string sql = "INSERT INTO [222_PartExchange] ([PartNo], [UsingUnit], [BuyingUnit],"
                        + " [Using/IQEx], [Buying/IQEx])"
                        + " VALUES ('" + part_no + "', '" + using_unit + "', '" + buying_unit + "',"
                        + " " + using_exchange + ", " + buying_exchange + ")";                    
                    SqlCommand cmd_sql = new SqlCommand(sql, myConnection);                    
                    cmd_sql.ExecuteNonQuery();
                    cmd_sql.Dispose();
                    myConnection.Close();
                    MessageBox.Show("Unit Exchange added success.");
                    clearForm();
                }
                catch //(Exception ex)
                {
                    if (myConnection.State == ConnectionState.Open)
                    {
                        myConnection.Close();
                    }
                    //MessageBox.Show(ex.Message);
                    MessageBox.Show("Cannot add Unit Exchange. Please try again.");
                }
            }
            else
            {
                MessageBox.Show(error);
            }
        }

        private void clearForm()
        {
            foreach (Control c in this.Controls)
            {
                if (c is DevExpress.XtraEditors.TextEdit)
                {
                    ((DevExpress.XtraEditors.TextEdit)c).Text = null;
                }

                if (c is DevExpress.XtraEditors.ComboBoxEdit)
                {
                    ((DevExpress.XtraEditors.ComboBoxEdit)c).SelectedIndex = -1;
                }

                if (c is System.Windows.Forms.MaskedTextBox)
                {
                    ((System.Windows.Forms.MaskedTextBox)c).Text = null;
                }
            }
        }

    }
}
